package com.shopping.service.impl;

import com.shopping.mapper.CartMapper;
import com.shopping.mapper.ProductMapper;
import com.shopping.pojo.Cart;
import com.shopping.pojo.Product;
import com.shopping.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceimpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addToCart(Integer customerId, Cart cart) {
        cart.setUserId(customerId);
        Product product=productMapper.getById(cart.getProductId());
        cart.setImage(product.getImage());
        cart.setName(product.getName());
        cart.setStatus(0);
        cart.setQuantity(1);
        int count = cartMapper.checkItemExists(cart.getUserId(), cart.getProductId(), cart.getStatus());
        if (count > 0) {
            cartMapper.updateCartItemQuantity(cart.getUserId(), cart.getProductId(), cart.getQuantity(), cart.getStatus());
        } else {
            cartMapper.insertCartItem(cart);
        }
    }

    //查询购物车商品数据
    @Override
    public List<Cart> getCartList(Integer customerId) {
        return cartMapper.getCartList(customerId);
    }
    //查询订单列表
    @Override
    public List<Cart> getOrderList(Integer customerId) {
        return cartMapper.getOrderList(customerId);
    }

    @Override
    public List<Cart> allOrder() {
        return cartMapper.getAllOrder();
    }

    //修改购物车商品数据
    @Override
    public void modifyCart( List<Cart> carts) {
        for (Cart cart : carts) {
            cartMapper.updateSingleCartItem(cart);
            cartMapper.deleteProductStock(cart);
        }
    }

    @Override
    public void deleteCart(Integer id) {
        cartMapper.deleteCart(id);
    }

}
